<template>
  <el-dialog
    class="assign-role"
    title="分配角色"
    :visible="showRoleDialog"
    @close="close"
  >
    <!-- 这里准备复选框 -->
    <el-checkbox-group v-model="roleIds">
      <!-- 多选的选项 -->
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
      <!-- <el-checkbox label="113">开发者</el-checkbox>
      <el-checkbox label="115">人事</el-checkbox> -->
    </el-checkbox-group>
    <!-- 底部 -->
    <template #footer>
      <el-button type="primary" size="small" @click="assignRoles">
        确定
      </el-button>
      <el-button size="small" @click="$emit('update:showRoleDialog')">
        取消
      </el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      roleIds: [], // 角色id
      roleList: [], // 角色列表
      userId: '' // 用户id
    }
  },
  created () {
    this.getRoleList()
  },
  methods: {
    // 关闭弹层执行
    close () {
      this.$emit('update:showRoleDialog')
    },
    // 获取角色列表
    async getRoleList () {
      // params:{page 页码, pagesize 每页几条}
      const { rows } = await getRoleList({ page: 1, pagesize: 1000 })
      // console.log('角色列表', rows)
      this.roleList = rows
    },
    // 获取选中的角色 id:角色ID
    async getUserDetailById (id) {
      const { roleIds } = await getUserDetailById(id)
      // console.log(roleIds)
      // 回显选中
      this.roleIds = roleIds || []
      //  存储用户ID
      this.userId = id
    },
    // 保存选中的角色
    async assignRoles () {
      /**
       * 1.点击确认按钮
       * 2.关闭弹层
       */
      await assignRoles({ id: this.userId, roleIds: this.roleIds })
      this.$emit('update:showRoleDialog')
    }
  }
}
</script>
